package cn.heapstack.jpa.entityrelationships.main;

import java.util.Date;
import java.util.UUID;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import cn.heapstack.jpa.entityrelationships.domain.Address;
import cn.heapstack.jpa.entityrelationships.domain.CreditCard;
import cn.heapstack.jpa.entityrelationships.domain.Customer;

public class MainForOneToOne {

	/**
	 * 这个例子用来验证 关联中的知识点
	 * @param args
	 */
	public static void main(String[] args) {
		EntityManagerFactory factory = Persistence
				.createEntityManagerFactory("EntityRelationships");
		EntityManager em = factory.createEntityManager();

		em.getTransaction().begin();

		String lastName = UUID.randomUUID().toString();

		Customer customer = new Customer();
		Address address1 = new Address();
		CreditCard creditCard = new CreditCard();
		
		address1.setCity("city");
		address1.setState("state");
		address1.setStreet("street");
		
		creditCard.setExpiration(new Date());
		creditCard.setName("name");
		creditCard.setNumber("number");
		creditCard.setOrgrnization("orgrnization");
		
		customer.setFirstName("firstName");
		customer.setLastName("lastName:"+lastName);
		customer.setAddress(address1);
		customer.setCreditCard(creditCard);
		creditCard.setCustomer(customer);
		
		em.persist(customer);
		
		/*
		//删除creditCard
		em.remove(card);
		customer = em.find(Customer.class, customer.getId());
		System.out.println("after em.remove(creditCard) ");
		System.out.println(customer.getCreditCard().getId());
		
		这个情况下，提交时会报出异常
		Caused by: org.hibernate.ObjectDeletedException: deleted entity passed to persist: [cn.heapstack.jpa.entityrelationships.domain.CreditCard#<null>]
		
		*/
		
		//将另一个CreditCard关联到customer
		CreditCard creditCard2 = new CreditCard();
		creditCard2.setExpiration(new Date());
		creditCard2.setName("name2");
		creditCard2.setNumber("number2");
		creditCard2.setOrgrnization("orgrnization2");
		
		//没啥影响
//		creditCard.setCustomer(null);
//		em.remove(creditCard);
		
		//客户关联新的信用卡
		customer.setCreditCard(creditCard2);
		creditCard2.setCustomer(customer);
		em.merge(customer);
		// END 
		
		
		
		//使用新的customer关联新的一个个creditCard
		Customer customer_new = new Customer();
		Address address2 = new Address();
		address2.setCity("city2");
		address2.setState("state2");
		address2.setStreet("street2");
		customer_new.setAddress(address2);
		customer_new.setFirstName("new_firstName");
		customer_new.setLastName("new_lastName"+lastName);
		customer_new.setCreditCard(creditCard2);
		creditCard2.setCustomer(customer_new);
		em.persist(customer_new);
		
		Customer c1 = em.find(Customer.class, customer.getId());
		Customer c2 = em.find(Customer.class, customer_new.getId());
		System.out.println("customer 1's credit card id:" + c1.getCreditCard().getId());
		System.out.println("customer 2's credit card id:" + c2.getCreditCard().getId());
		//其实两个Customer关联的creditCard信息一样，只不过id不同
		
		System.out.println("customer_new's id:" + customer_new.getId());
		System.out.println("remove customer_new from database");	
		em.remove(customer_new);
		CreditCard cardTemp = em.find(CreditCard.class, customer_new.getCreditCard().getId());
		if(cardTemp != null)
		{
			System.out.println("customer_new's credit card exist ? :" + cardTemp.getId());
		}
		else
		{
			System.out.println("customer_new's credit card do not exist any more");
		}
		// One-to-One(bidirection) end
		//*****************************************************************************************
		
		
		
		
		em.getTransaction().commit();
		em.close();
		factory.close();

	}

}
